package webModule.product
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Circ;
	import com.touchX.display.FreedomDisplayObject;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class ProductView extends FreedomDisplayObject
	{
		private var _module:ProductModule;
		private const margin:int=10;
		private var _img:Bitmap;
		private var _label:Label;
		private var _dragCircle:Sprite;
		public var closeLeft:Boolean;
		
		public function ProductView(module:ProductModule)
		{
			super();
			this.buttonMode=true;
			this._module=module;
		}
		protected override function added(event:Event):void{
			super.added(event);
			init();
			addEvent(this,MouseEvent.CLICK,showThisModule);
		}
		private function init():void{
			this.graphics.beginFill(0xeeeeee);
			_img=_module.thumbnail;
			this.graphics.drawRect(0,0,_img.width+2*margin,_img.height+2*margin);
			this.graphics.endFill();
			_img.x=_img.y=margin;
			this.addChild(_img);
			
			_label=new Label(this.width,30,0xeeeeee,margin);
			_label.label=_module.productName;
			this.addChildAt(_label,0);
			
			_dragCircle=new Sprite();
			_dragCircle.graphics.beginFill(0xffcc00);
			_dragCircle.graphics.drawCircle(0,0,5);
			_dragCircle.graphics.endFill();
			_dragCircle.y=_dragCircle.x=margin;
			_dragCircle.visible=false;
			addChild(_dragCircle);
			_dragCircle.addEventListener(MouseEvent.MOUSE_DOWN,dragEnable);
			_dragCircle.addEventListener(MouseEvent.MOUSE_UP,disableDrag);
		}
		
	
		public override function dispose():void{
			super.dispose();
			removeEvent(this,MouseEvent.CLICK,showThisModule);
		}
		private function showThisModule(evt:MouseEvent):void{
			if(evt.target is ProductView){
				_module.dispatchEvent(evt);
			}
		}
		protected override function resize(event:Event=null):void{
			if(!closeLeft){
				super.resize(event);
			}
		}
		private function disableDrag(evt:MouseEvent):void{
			this.stopDrag();
			this.initPoint=new Point(this.x,this.y);
		}
		private function dragEnable(evt:MouseEvent):void{
			this.startDrag();
			evt.stopImmediatePropagation();
		}
		protected override function activing(evt:MouseEvent):void{
			super.activing(evt);
			_label.y=_img.y+_img.height;
			_dragCircle.visible=true;
		}
		protected override function disactive(evt:MouseEvent):void{
			super.disactive(evt);
			_label.y=margin;
			_dragCircle.visible=false;
		}
	}
}

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

class Label extends Sprite{
	private var txtField:TextField;
	
	public function Label(w:int,h:int,col:uint,margin:int){
		this.graphics.beginFill(col);
		this.graphics.drawRect(0,0,w,h);
		this.graphics.endFill();
		
		var format:TextFormat=new TextFormat();
		
		txtField=new TextField()
		txtField.mouseEnabled=false;
		txtField.autoSize=TextFieldAutoSize.LEFT;
		txtField.x=margin;
		txtField.y=margin/2;
		addChild(txtField);
		
	}
	
	public function set label(value:String):void{
		txtField.text=value;
	}
	
}